package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.AbnormalConsumptionDTO;
import com.quanquan.management.entity.SysUser;
import com.quanquan.management.request.AbnormalConsumptionRequest;
import entity.ExcelData;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import utils.ExcelUtil;
import utils.Request;
import utils.StringUtil;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName:AbnormalConsumptionService
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/8/26 10:16
 * @Version 1.0
 */
@Service
public class AbnormalConsumptionService {

    @Value("${module-url.userUrl}")
    private String userUrl;

    public PageDTO<AbnormalConsumptionDTO> list(AbnormalConsumptionRequest requestBody) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        requestBody.setSysUserId(sysUser.getId());
        String json = Request.postJson(userUrl + "abnormal_consumption/query_menegement", requestBody);
        PageDTO<AbnormalConsumptionDTO> pageDTO = new PageDTO<>();
        if (StringUtil.isEmpty(json)) {
            return pageDTO;
        }
        JSONObject jsonObject = JSON.parseObject(json);
        if (null == jsonObject) {
            return pageDTO;
        }
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<AbnormalConsumptionDTO> userDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++) {
                AbnormalConsumptionDTO object = list.getObject(i, AbnormalConsumptionDTO .class);
                userDTOS.add(object);
            }
            data.setList(userDTOS);
            return data;
        }
        return pageDTO;
    }

    public void exportSendCouponRecord(HttpServletResponse response, AbnormalConsumptionRequest requestBody) throws Exception {
        List<String> titles = new LinkedList<>();
        titles.add("用户名");
        titles.add("手机号");
        titles.add("消费笔数");
        titles.add("商户名称");
        titles.add("记录时间");
        ExcelData data = ExcelUtil.getExcelData();
        data.setTitles(titles);
        PageDTO<AbnormalConsumptionDTO> pageDTO = this.list(requestBody);
        List<AbnormalConsumptionDTO> list = pageDTO.getList();
        List<List<Object>> rows = new LinkedList<>();
        list.forEach(codeDTO -> {
            List<Object> row = new LinkedList<>();
            row.add(codeDTO.getUserName());
            row.add(codeDTO.getPhone());
            row.add(codeDTO.getCount());
            row.add(codeDTO.getStoreName());
            row.add(codeDTO.getCreateTime());
            rows.add(row);
        });
        data.setRows(rows);
        String now = DateUtils.dateToString(new Date(), DateUtils.DATE_HOUR_SECOND);
        ExcelUtil.exportExcel(response, "会员异常积分报表-" + now, data);
    }



}
